<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2017-09-24
 * Time: 15:43
 */

namespace app\models;


use app\librarys\DbCacheUtility;
use Codeception\Module\Db;

class Agent
{
    public static function getCount()
    {
        $cmd = \Yii::$app->db->createCommand('SELECT count(1) FROM t_agent WHERE is_deleted = 0');
        $count = DbCacheUtility::queryScalar($cmd);
        return $count;
    }

    public static function getList($page, $pageSize)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT t1.*,t2.area_name as agent_province_name,t3.area_name as agent_city_name,t4.area_name as agent_county_name FROM t_agent t1
                                                  LEFT JOIN t_area t2 ON t2.id = t1.agent_province_id
                                                  LEFT JOIN t_area t3 on t3.id = t1.agent_city_id
                                                  LEFT JOIN t_area t4 ON t4.id = t1.agent_county_id 
                                              WHERE t1.is_deleted = 0 ORDER BY t1.create_datetime DESC limit :offset , :limit');
        $cmd->bindValue(':offset', $page * $pageSize);
        $cmd->bindValue(':limit', $pageSize);
        $items = DbCacheUtility::queryAll($cmd);
        return $items;
    }

    public static function getById($id)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_agent WHERE id = :id');
        $cmd->bindValue(':id', $id);
        $item = DbCacheUtility::queryOne($cmd);
        return $item;
    }

    public static function save($id, $agent_name, $agent_province_id, $agent_city_id, $agent_county_id, $is_enabled)
    {
        $item = self::getById($id);
        if ($item) {
            //update
            //每个代理商1个地区，修改的时候不能重复
            $sql = 'SELECT count(1) FROM t_agent WHERE id <> :id AND agent_province_id = :agent_province_id AND agent_city_id = :agent_city_id AND agent_county_id = :agent_county_id';
            $cmd = \Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':id', $id);
            $cmd->bindValue(':agent_province_id', $agent_province_id);
            $cmd->bindValue(':agent_city_id', $agent_city_id);
            $cmd->bindValue(':agent_county_id', $agent_county_id);
            $count = DbCacheUtility::queryScalar($cmd);
            if ($count > 0) {
                return false;
            } else {
                $sql = 'UPDATE t_agent SET agent_name = :agent_name, agent_province_id = :agent_province_id , agent_city_id = :agent_city_id,agent_county_id = :agent_county_id, is_enabled = :is_enabled WHERE id = :id';
                $cmd = \Yii::$app->db->createCommand($sql);
                $cmd->bindValue(':id', $id);
                $cmd->bindValue(':agent_name', $agent_name);
                $cmd->bindValue(':agent_province_id', $agent_province_id);
                $cmd->bindValue(':agent_city_id', $agent_city_id);
                $cmd->bindValue(':agent_county_id', $agent_county_id);
                $cmd->bindValue(':is_enabled', $is_enabled);

                $result = DbCacheUtility::execute($cmd);
                if ($result > 0) {
                    return true;
                }
                return false;
            }
        }

        //每个代理商1个地区，添加的时候不能重复
        $sql = 'SELECT count(1) FROM t_agent WHERE agent_province_id = :agent_province_id AND agent_city_id = :agent_city_id AND agent_county_id = :agent_county_id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':agent_province_id', $agent_province_id);
        $cmd->bindValue(':agent_city_id', $agent_city_id);
        $cmd->bindValue(':agent_county_id', $agent_county_id);
        $count = DbCacheUtility::queryScalar($cmd);
        if ($count > 0) {
            return false;
        } else {
            $sql = 'INSERT INTO t_agent (id, agent_name, agent_province_id, agent_city_id,agent_county_id, is_enabled, is_deleted) 
                    VALUES (uuid(), :agent_name, :agent_province_id, :agent_city_id,:agent_county_id, :is_enabled, 0)';
            $cmd = \Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':agent_name', $agent_name);
            $cmd->bindValue(':agent_province_id', $agent_province_id);
            $cmd->bindValue(':agent_city_id', $agent_city_id);
            $cmd->bindValue(':agent_county_id', $agent_county_id);
            $cmd->bindValue(':is_enabled', $is_enabled);

            $result = DbCacheUtility::execute($cmd);
            if ($result > 0) {
                return true;
            }
        }

        return false;
    }

    public static function updateStatus($id)
    {
        $sql = 'UPDATE t_agent SET is_deleted = 1- is_deleted WHERE id = :id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);
        $result = DbCacheUtility::execute($cmd);
        return $result > 0;
    }


    public static function getAgentIdByMerchantId($merchant_id)
    {
        $sql = 'SELECT t1.* FROM t_agent t1 INNER JOIN t_merchant t2 ON t1.agent_province_id = t2.province AND t1.agent_city_id = t2.city AND t1.agent_county_id = t2.county
                WHERE t2.id = :id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $merchant_id);
        $item = DbCacheUtility::queryOne($cmd);
        if ($item) {
            return $item['id'];
        }
        return null;
    }

    public static function deleteAgentById($id){
        $sql = 'delete from t_agent WHERE id = :id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);
        $item = DbCacheUtility::execute($cmd);
        if ($item) {
            return $item['id'];
        }
        return null;
    }
}